package com.example.xpvideo.service.serviceImpl;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.xpvideo.context.PageContext;
import com.example.xpvideo.entity.*;
import com.example.xpvideo.entity.req.*;
import com.example.xpvideo.entity.res.LocalGoodsRes;
import com.example.xpvideo.entity.res.SeckillGoodsDetail;
import com.example.xpvideo.entity.res.SeckillGoodsRes;
import com.example.xpvideo.enums.ErrCodeEnum;
import com.example.xpvideo.exception.ErrorException;
import com.example.xpvideo.mapper.LsSeckillGoodsMapper;
import com.example.xpvideo.mapper.LsSeckillTimeMapper;
import com.example.xpvideo.mapper.SeckillGoodsMapper;
import com.example.xpvideo.service.LsSeckillTimeService;
import com.example.xpvideo.service.SeckillGoodsService;
import com.example.xpvideo.service.TimeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.List;

@Service
public class SeckillGoodsServiceImpl implements SeckillGoodsService {
    @Autowired
    private SeckillGoodsMapper seckillGoodsMapper;
    @Autowired
    private LsSeckillGoodsMapper lsSeckillGoodsMapper;
    @Autowired
    private LsSeckillTimeMapper lsSeckillTimeMapper;
    @Autowired
    private TimeService timeService;

    @Override
    public ResponseResult<PageResult<List<SeckillGoodsRes>>> list(SecckillGoodsReq secckillGoodsReq) throws ErrorException {
        String name = secckillGoodsReq.getName();
        LocalDate startDate = secckillGoodsReq.getStartDate();
        LocalDate endDate = secckillGoodsReq.getEndDate();
        String startTime = secckillGoodsReq.getStartTime();
        String endTime = secckillGoodsReq.getEndTime();
        Integer reviewStatus = secckillGoodsReq.getReviewStatus();

        int page = PageContext.getPage();
        int size = PageContext.getSize();

        PageHelper.startPage(page, size);

        List<SeckillGoodsRes> list = seckillGoodsMapper.SeckillGoodsList(Wrappers.<LsSeckillGoods>query()
                        .like(!ObjectUtils.isEmpty(name),"name",name)
                        .ge(!ObjectUtils.isEmpty(startDate),"start_date",startDate)
                        .le(!ObjectUtils.isEmpty(endDate),"end_date",endDate)
                        .ge(!ObjectUtils.isEmpty(startTime),"start_time",startTime)
                        .le(!ObjectUtils.isEmpty(endTime),"end_time",endTime)
                        .eq(!ObjectUtils.isEmpty(reviewStatus),"review_status",reviewStatus)
                        .eq("lsg.del",0)
                .groupBy("goods_id")
                .orderByDesc("price"));

        PageInfo pageInfo = new PageInfo<>(list);
        //当前页
        int pageNum = pageInfo.getPageNum();
        //总页数
        int pages = pageInfo.getPages();
        //当前页展示的数量
        int pageSize = pageInfo.getSize();
        //查询总数
        long count = pageInfo.getTotal();
        PageResult<List<SeckillGoodsRes>> listPageResult = new PageResult<>(count, pages, pageSize, pageNum, list);

        return ResponseResult.responseResult(0, listPageResult);

    }

    @Override
    public ResponseResult<SeckillGoodsDetail> detail(Integer id) throws ErrorException{
        SeckillGoodsDetail seckillGoodsDetail = seckillGoodsMapper.SeckillGoodsDetail(Wrappers.<LsSeckillGoods>query()
                .eq("lsg.id",id));

        return ResponseResult.responseResult(0,"",seckillGoodsDetail);
    }

    @Override
    public ResponseResult<Object> insertTime(InsertSeckillTime insertSeckillTime) throws ErrorException{
        String startTime = insertSeckillTime.getStartTime();
        String endTime = insertSeckillTime.getEndTime();

        LsSeckillTime lsSeckillTime = new LsSeckillTime();
        lsSeckillTime.setStartTime(startTime);
        lsSeckillTime.setEndTime(endTime);
        lsSeckillTime.setDel(0);
        lsSeckillTime.setCreateTime(timeService.getTime());
        int res = lsSeckillTimeMapper.insertLsSeckillTime(lsSeckillTime);

        if(res > 0){
            return ResponseResult.responseResult(0,"插入时间段成功");
        }
        throw new ErrorException(ErrCodeEnum.unknown_20001);
    }

    @Override
    public ResponseResult<Object> deleteTime(UpdateReq updateReq) throws ErrorException{
        Integer id = updateReq.getId();
        Integer status = updateReq.getStage();

        LsSeckillTime lsSeckillTime = new LsSeckillTime();
        lsSeckillTime.setId(id);
        lsSeckillTime.setDel(status);
        lsSeckillTime.setUpdateTime(timeService.getTime());
        int res = lsSeckillTimeMapper.updateLsSeckillTime(lsSeckillTime);

        if(res > 0){
            return ResponseResult.responseResult(0, "删除成功");
        }
        throw new ErrorException(ErrCodeEnum.unknown_20003);
    }

    @Override
    public ResponseResult<PageResult<List<LsSeckillTime>>> timeList()throws ErrorException{
        int page = PageContext.getPage();
        int size = PageContext.getSize();

        PageHelper.startPage(page, size);

        List<LsSeckillTime> list = lsSeckillTimeMapper.selectList(Wrappers.<LsSeckillTime>query()
                .orderByDesc("create_time"));

        PageInfo pageInfo = new PageInfo<>(list);
        //当前页
        int pageNum = pageInfo.getPageNum();
        //总页数
        int pages = pageInfo.getPages();
        //当前页展示的数量
        int pageSize = pageInfo.getSize();
        //查询总数
        long count = pageInfo.getTotal();
        PageResult<List<LsSeckillTime>> listPageResult = new PageResult<>(count, pages, pageSize, pageNum, list);

        return ResponseResult.responseResult(0, listPageResult);
    }

    @Override
    public ResponseResult<Object> update(LsSeckillGoods lsSeckillGoods) throws ErrorException{
        Integer id = lsSeckillGoods.getId();
        Integer seckillId = lsSeckillGoods.getSeckillId();
        BigDecimal price = lsSeckillGoods.getPrice();
        String startDate = lsSeckillGoods.getStartDate();
        String endDate = lsSeckillGoods.getEndDate();

        LsSeckillGoods lsSeckillGoods1 = new LsSeckillGoods();
        lsSeckillGoods1.setId(id);
        lsSeckillGoods1.setSeckillId(seckillId);
        lsSeckillGoods1.setPrice(price);
        lsSeckillGoods1.setStartDate(startDate);
        lsSeckillGoods1.setEndDate(endDate);
        lsSeckillGoods1.setUpdateTime(timeService.getTime());

        int res = lsSeckillGoodsMapper.updateLsSeckillGoods(lsSeckillGoods1);

        if(res > 0){
            return ResponseResult.responseResult(0, "编辑商品成功");
        }

        throw new ErrorException(ErrCodeEnum.unknown_20000);
    }

    @Override
    public ResponseResult<Object>delete(UpdateReq updateReq) throws ErrorException{
        Integer id = updateReq.getId();
        Integer del = updateReq.getStage();

        LsSeckillGoods lsSeckillGoods = new LsSeckillGoods();
        lsSeckillGoods.setId(id);
        lsSeckillGoods.setDel(del);
        lsSeckillGoods.setUpdateTime(timeService.getTime());
        int res = lsSeckillGoodsMapper.updateLsSeckillGoods(lsSeckillGoods);

        if(res > 0){
            return ResponseResult.responseResult(0, "删除商品成功");
        }

        throw new ErrorException(ErrCodeEnum.unknown_20003);
    }

    @Override
    public ResponseResult<Object> audit(AuditReq auditReq) throws ErrorException{
        Integer id = auditReq.getId();
        Integer reviewStatus = auditReq.getStatus();
        String remark = auditReq.getRemark();

        LsSeckillGoods lsSeckillGoods = new LsSeckillGoods();
        lsSeckillGoods.setId(id);
        lsSeckillGoods.setReviewStatus(reviewStatus);
        lsSeckillGoods.setReviewDesc(remark);
        lsSeckillGoods.setUpdateTime(timeService.getTime());
        int res = lsSeckillGoodsMapper.updateLsSeckillGoods(lsSeckillGoods);

        if(res > 0) {
            return ResponseResult.responseResult(0, "审核秒杀商品成功");
        }
        throw new ErrorException(ErrCodeEnum.unknown_20000);
    }
}
